﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;
using System.Text;
using System.Data;

namespace Portal
{
    public partial class SelectGroups : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }

        //定义Json对象
        string rtnJson = "";

        //初始化加载
        void DataBind() {

            StringBuilder StrSQL = new StringBuilder();
            StrSQL.AppendFormat(@"select s.id,s.groupname from sys_groups s");
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSQL.ToString());
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                TreeNode node = new TreeNode();
                node.Text = dt.Rows[i]["groupname"].ToString();
                node.Value = dt.Rows[i]["id"].ToString();
                TvGroups.Nodes.Add(node);
            }
        }

        //选中项事件
        protected void TvGroups_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
          
        }

        //单击传送Json
        protected void btnSure_Click(object sender, EventArgs e)
        {
            rtnJson = "[";
            IterateTreeView(TvGroups.Nodes);
            rtnJson = rtnJson.TrimEnd(new char[] { ',' });
            rtnJson += "]";
            Page.RegisterStartupScript("RtnVal", "<script>window.returnValue =" + rtnJson + ";window.close();</script>");
        }

        //读取被选中的组
        public void IterateTreeView(TreeNodeCollection tnc)
        {
            foreach (TreeNode tn in tnc)
            {
                if (tn.Checked)
                {
                    // strText+=""
                    rtnJson += "{'GroupName':'" + tn.Text.ToString() + "',";
                    rtnJson += "'GroupId':'" + tn.Value.ToString() + "'},";
                }
                IterateTreeView(tn.ChildNodes);
            }
        }
    }
}